package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import liumengbaio.lolzhangyoubao.Activity.InFo.MoreTreasureInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * “我的夺宝”界面适配器
 * Created by lanouhn on 16/9/7.
 */
public class MoreTreasureAdapter extends BaseAdapter {

    private Context mContext;
    private MoreTreasureInfo mMoreTreasureInfo;


    public MoreTreasureAdapter(Context mContext, MoreTreasureInfo mMoreTreasureInfo) {
        this.mContext = mContext;
        this.mMoreTreasureInfo = mMoreTreasureInfo;
    }

    @Override
    public int getCount() {
        return mMoreTreasureInfo.getData().size();
    }

    @Override
    public Object getItem(int position) {
        return mMoreTreasureInfo.getData().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_more_treasure, null);
            holder.iv = (ImageView) convertView.findViewById(R.id.more_treasure_join);
            holder.pic = (ImageView) convertView.findViewById(R.id.more_treasure_pic);
            holder.unit = (TextView) convertView.findViewById(R.id.more_treasure_unit);
            holder.title = (TextView) convertView.findViewById(R.id.more_treasure_title);
            holder.goldAmount = (TextView) convertView.findViewById(R.id.more_treasure_goldAmount);
            holder.surplusAmount = (TextView) convertView.findViewById(R.id.more_treasure_surplusAmount);
            holder.mProgressBar = (ProgressBar) convertView.findViewById(R.id.more_treasure_progressBar);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /**
         * 通过实体类得到相关数据
         */
        Picasso.with(mContext).load(mMoreTreasureInfo.getData().get(position).getGoods_url()).into(holder.pic);
        holder.unit.setText(mMoreTreasureInfo.getData().get(position).getUnit_name());
        holder.title.setText(mMoreTreasureInfo.getData().get(position).getName());
        holder.goldAmount.setText(mMoreTreasureInfo.getData().get(position).getPrice() + "");
        holder.surplusAmount.setText(mMoreTreasureInfo.getData().get(position).getRaid_remain() + "");
        /**
         * 设置progressBar，显示物品的信息
         */
        holder.mProgressBar.setMax(mMoreTreasureInfo.getData().get(position).getRaid_amount());
        holder.mProgressBar.setProgress(mMoreTreasureInfo.getData().get(position).getRaid_total());
        /**
         * 判断该物品要显示的状态
         */
        if (mMoreTreasureInfo.getData().get(position).getStatus() == 1) {
            holder.iv.setBackgroundResource(R.mipmap.more_treasure_join);
        } else {
            holder.iv.setBackgroundResource(R.mipmap.more_treasure_end);
        }
        return convertView;
    }

    private class ViewHolder {
        private ImageView pic, iv;
        private TextView title, goldAmount, unit, surplusAmount;
        private ProgressBar mProgressBar;
    }
}
